"""author: wenyao
   data: 2020/11/3
   project: ludingji
"""
#出现错误异常，程序就立即终止
# print("start......")
# f = open("test.txt")
# print("end......")

#常见异常
#nameerror
# print(b)

#ZeroDivisionError 除数为0
# a = 1/0

#IndentationError: unexpected indent 缩进错误
  # print("ok")

#SyntaxError: invalid syntax 语法错误
# if 1>0::
#    print("ok")

#IndexError: list index out of range  list的下标超出了范围
# a=[1,2,3]
# print(a[4])

#keyerror KeyError: 'c', 推荐使用get属性获取值
# a = {"a":1,"b":2}
# print(a["c"], a.get("c",0))

#AttributeError: module 'math' has no attribute 'xxx'
# import math
# math.xxx()

#valueError 传递的参数类型不正确等
# int("abc")

#try...except 捕获
#except 可以有多个
#except捕获的错误与程序触发的错误不一致，程序就会捕获不到
#except 匹配到第一个就退出
#如果父类异常在最前面，会吞噬所有的子类异常
# try:
#     print("start try......")
#     a = 1/0
#     print("end try.....")
# except IndexError as ie:
#     print("index error:",ie)
# except ZeroDivisionError as ze:
#     print("zero error:",ze)
# except Exception as e:
#     print("exception error")
# print("end................")

# case $num in
# 1)
# 2)
# *)
# try:
#     a = 1/0
# except Exception as e:
#     print("exception:",e)
# except ZeroDivisionError as ze:
#     print("zero error:",ze)
# print("end......")

#else结构
#没有发生任何异常时处理else的语句块
# try:
#     print("start try....")
#     a = 1/0
# except Exception as e:
#     print("exception error")
# else:
#     print("this is else test")

#finally
#不管有没有异常，不管异常有没有被捕获都执行
# try:
#     print("start try....")
#     a = [1,2,3,4]
#     print(a[5])
# except IndexError as ie:
#     print("index error")
# finally:
#     print("this is finally test")

# try:
#     #a = 1/0
#     print("start .......")
#     a = [1]
#     print(a[2])
# except ZeroDivisionError as ze:
#     print("zero error")
# else:
#     print("code ok")
# finally:
#     print("finally")

#raise 手动触发异常
#raise  异常类("说明信息")
#raise 没有接异常类，默认抛出RuntimeError异常
# try:
#      print("raise error")
#      raise IndexError("this is test error")
#      print("raise error end....")
# except RuntimeError as e:
#     print("raise excetion")
#     print(e)
# except Exception as  e:
#     print("excetion error")
#     print(e)


# try:
#     a = int(input("输入一个0~100的整数"))
#     if a > 100 :
#         raise IndexError
#     if a < 0:
#         raise RuntimeError
# except ValueError as va:
#     print("你输入的不是整数")
# except IndexError:
#     print("你输入的数字大于100")
# except RuntimeError:
#     print("你输入的数字小于0")

#嵌套异常
# def aa():
#     try:
#         a = "hello world"
#         try:
#             int(a)
#             #a = "ac"
#         except ValueError as  e:
#             print("Value Error")
#             return "i am value error"
#         finally:
#             print("i am finally ")
#         print("xxxxxxxxxxxxxxxxxxx")
#         b = [1,2,3]
#         print(b[3])
#     except IndexError as msg:
#         print("Index Error")
#     finally:
#         print("i am index error finally")
# b = aa()
# print("--------------")
# print(b)

#try:
#    语句块
# except
#    语句块

# try:
#     int("abc")
# finally:
#     print("test finally")

